﻿var srcObj = {
	player:"player4.png",
	ground: "ground.png",
	stone2:"stone4.png",
	stone1:"stone1.png",
	wall:"wall4.png",
	wall1:"wall1.png",
	player2:"player3.png"
};
var time1 = new Date().getTime();
var time2 = new Date().getTime();
var mapMatrix = [
	[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2],
	[2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
	[2,0,1,1,1,1,0,1,0,0,1,0,1,1,1,0,0,2],
	[2,0,1,0,0,1,0,1,0,0,1,0,1,0,0,1,0,2],
	[2,0,0,0,0,1,0,1,0,0,1,0,1,0,0,1,0,2],
	[2,0,1,1,1,1,0,1,1,1,1,0,1,1,1,0,0,2],
	[2,0,1,0,0,0,0,0,0,0,1,0,1,0,0,1,0,2],
	[2,0,1,0,0,1,0,0,0,0,1,0,1,0,0,1,0,2],
	[2,0,1,1,1,1,0,1,1,1,1,0,1,1,1,0,0,2],
	[2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
	[2,0,0,1,0,0,0,1,0,2,2,2,0,0,2,2,2,2],
	[1,0,0,1,1,0,0,1,0,2,0,2,0,0,2,0,2,2],
	[2,0,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,2],
	[2,0,0,1,0,0,1,1,0,0,0,1,0,0,1,0,0,2],
	[2,0,0,1,0,0,0,1,0,0,0,1,1,1,1,0,0,2],
	[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]
];

cnGame.init("canvas", { width: 460, height: 320 });


var player = function(options) {
		this.init(options);
		this.FOV = options.FOV || 60; //玩家的视野角度
	this.bodyHeight=20;//玩家视觉高度
	this.moveSpeed=2.5;
}
cnGame.core.inherit(player, cnGame.Sprite);

var isOnWall=function(x,y,width,height,map){
return map.getPosValue(x+width/2,y+height/2);
/*
	return map.getPosValue(x+5+width/2,y+5+height/2) 
			 && map.getPosValue(x+5+width/2,y-5+height/2) 
			 && map.getPosValue(x-5+width/2,y+5+height/2) 
			 && map.getPosValue(x-5+width/2,y-5+height/2);
	*/
}

var drawColLine=function(){
	var screenX;
	var colAngle;
	var centerX,centerY,beforeX,beforeY,x,y,distant,heightInScreen;
	var colCount=this.screenSize[0]/this.viewColWidth;
	var context=this.screenContext;

	context.clearRect(0,0,this.screenSize[0],this.screenSize[1]);
	context.fillStyle="rgb(203,242,238)";
	context.fillRect(0,0,this.screenSize[0],this.screenSize[1]/2);
	context.fillStyle="rgb(77,88,87)";
	context.fillRect(0,this.screenSize[1]/2,this.screenSize[0],this.screenSize[1]/2);
	
		time1 = new Date().getTime();
		var timeDiff=time1-time2
		document.getElementById("fps").innerHTML="FPS:"+parseInt(1000/timeDiff)+"			(X:"+parseInt(this.player.x)+", Y:"+parseInt(this.player.y)+")";
		time2 = time1;
	//cnGame.context.beginPath();
	for(var index=0,colCount=this.screenSize[0]/this.viewColWidth;index<colCount;index+=1){
		screenX=-this.screenSize[0]/2+index*this.viewColWidth;//该竖线在屏幕的x坐标	
		colAngle=Math.atan(screenX/this.screenDistant);//玩家的视线到屏幕上的竖线所成的角度
		colAngle%=2*Math.PI;
		var angle=this.player.angle/180*(Math.PI)-colAngle;//射线在地图内所成的角度
		angle%=2*Math.PI;
		if(angle<0){
			angle+=2*Math.PI;
		}
		distant=20;
		x=0;
		y=0;
		var speed=0;
		centerX=this.player.x+(this.player.width)/2;//玩家中点X坐标
		centerY=this.player.y+(this.player.height)/2;//玩家中Y坐标
		while(this.map.getPosValue(centerX+x,centerY-y)==0){
			speed=0.4 + distant*0.008;
			// 修改这个参数能起到调节画质的作用
			distant+=speed;
			x=distant*Math.cos(angle);
			y=distant*Math.sin(angle);
		}
		ys = this.map.getPosValue(centerX+x,centerY-y);
		while(this.map.getPosValue(centerX+x,centerY-y)!=0){
			// 修改这个参数能起到调节画质的作用
			distant-=speed*0.065;
			x=distant*Math.cos(angle);
			y=distant*Math.sin(angle);
		}

		//如果射线在地图遇到墙壁,则画线
		/*cnGame.context.strokeStyle="#000";	
		cnGame.context.moveTo(centerX,centerY);
		cnGame.context.lineTo(centerX+x,Math.floor(centerY-y));
		cnGame.context.closePath();*/
		
		distant*=Math.cos(colAngle);//防止鱼眼效果
	
		heightInScreen=this.screenDistant/(distant)*this.wallSize[2];//根据玩家到墙壁的距离计算墙壁在视觉平面的高度
		
		var img=cnGame.loader.loadedImgs[srcObj.stone2];
		if (ys == 1) {
				img=cnGame.loader.loadedImgs[srcObj.stone1];
		}
		context.drawImage(img,0,0,2,240,this.viewColWidth*index,(this.screenSize[1]-heightInScreen)/2, this.viewColWidth,heightInScreen);
	}
	context.drawImage(cnGame.loader.loadedImgs[srcObj.player2],180,140,94,100);//画出玩家
}

var gameObj = {
	screenSize:[320,240],//视觉屏幕尺寸
	viewColWidth:1,//每条绘制线条的宽
	wallSize:[20,20,20],//墙的尺寸
		initialize: function() {
		cnGame.input.preventDefault(["left","up","down","right"]);
		this.screenDistant=(this.screenSize[0]/2)/(Math.sqrt(3)/3);//屏幕离玩家的距离
		this.screenCanvas=cnGame.core.$("screenCanvas");//用于显示3d空间的canvas
		this.screenContext=this.screenCanvas.getContext("2d");

		this.player=new player({src:srcObj.player,width:10,height:10,angle:90,x:20,y:290});
		cnGame.spriteList.add(this.player);
		this.map=new cnGame.Map(mapMatrix,{cellSize:[20,20],width:cnGame.width,height:cnGame.height});
		},
		update: function() {
		var input=cnGame.input;
		if(input.isPressed("left")){
			this.player.angle+=5;	
		}
		else if(input.isPressed("right")){
			this.player.angle-=5;
		}
		if(input.isPressed("up")){
			var nextX=Math.cos(this.player.angle*Math.PI/180)*this.player.moveSpeed;
			var nextY=Math.sin(this.player.angle*Math.PI/180)*this.player.moveSpeed;
			if(!isOnWall(this.player.x+nextX*2,this.player.y-nextY*2,this.player.width,this.player.height,this.map)
					&& !isOnWall(this.player.x+nextX,this.player.y-nextY,this.player.width,this.player.height,this.map)
				){
				this.player.x=this.player.x+nextX;
				this.player.y=this.player.y-nextY;
			}
		}
		else if(input.isPressed("down")){
			var nextX=Math.cos(this.player.angle*Math.PI/180)*this.player.moveSpeed;
			var nextY=Math.sin(this.player.angle*Math.PI/180)*this.player.moveSpeed;
			if(!isOnWall(this.player.x-nextX*2,this.player.y+nextY*2,this.player.width,this.player.height,this.map)
					&& !isOnWall(this.player.x-nextX,this.player.y+nextY,this.player.width,this.player.height,this.map)
				){
				this.player.x=this.player.x-nextX;
				this.player.y=this.player.y+nextY;
			}
		}
		},
		draw: function() {
		this.map.draw({"0":{src:srcObj.ground},"1":{src:srcObj.wall1},"2":{src:srcObj.wall}});
		drawColLine.call(this);
		
		}
};
cnGame.loader.start(gameObj,{srcArray:[srcObj.player,srcObj.player2,srcObj.ground,srcObj.wall,srcObj.stone2,srcObj.wall1,srcObj.stone1]});